
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               first_stage2.m
%
% Author:             Miguel R. Rueda and Sergio Ascencio
%
% Description:        It computes expected first stage probabilities (3 players) 
%
% Language:           MATLAB R2013b (8.2.0.701) 64 Bit
%
% Related Reference:   "Representatives"
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [phats,betas]=first_stage2(Y,X,CLv_PAN,CLv_PRI,CLv_PRD)


%Extended
% varsx={'l_d_RC_others' 'l_margin' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'l_diff_PRI_PRD' 'l_diff_PRI_PRD' 'lpop' 'l_turnout' 'local' 'ldist_city' ...
% 'l_d_RC_others_sq' 'l_margin_sq' 'lcasillas_sq' 'school_ic_sq' 'l_diff_PRI_PAN_sq' 'l_diff_PRI_PRD_sq' 'l_diff_PAN_PRD_sq' 'lpop_sq' 'l_turnout_sq' 'ldist_city_sq' ...
% 'l_d_RC_others_l_margin' 'l_d_RC_others_lcasillas' 'l_d_RC_others_school_ic' 'l_d_RC_others_l_diff_PRI_PAN' 'l_d_RC_others_l_diff_PRI_PRD' 'l_d_RC_others_l_diff_PAN_PRD' 'l_d_RC_others_lpop' 'l_d_RC_others_l_turnout' 'l_d_RC_others_local' 'l_d_RC_others_ldist_city' ...
% 'l_margin_lcasillas' 'l_margin_school_ic' 'l_margin_l_diff_PRI_PAN' 'l_margin_l_diff_PRI_PRD' 'l_margin_l_diff_PAN_PRD' 'l_margin_lpop' 'l_margin_l_turnout' 'l_margin_local' 'l_margin_ldist_city' ...
% 'lcasillas_school_ic' 'lcasillas_l_diff_PRI_PAN' 'lcasillas_l_diff_PRI_PRD' 'lcasillas_l_diff_PAN_PRD' 'lcasillas_lpop' 'lcasillas_l_turnout' 'lcasillas_local' 'lcasillas_ldist_city' ...
% 'school_ic_l_diff_PRI_PAN' 'school_ic_l_diff_PRI_PRD' 'school_ic_l_diff_PAN_PRD' 'school_ic_lpop' 'school_ic_l_turnout' 'school_ic_local' 'school_ic_ldist_city' ...
% 'l_diff_PRI_PAN_lpop' 'l_diff_PRI_PAN_l_turnout' 'l_diff_PRI_PAN_local' 'l_diff_PRI_PAN_ldist_city' 'l_diff_PRI_PRD_lpop' 'l_diff_PRI_PRD_l_turnout' 'l_diff_PRI_PRD_local' 'l_diff_PRI_PRD_ldist_city' ...
% 'l_diff_PAN_PRD_lpop' 'l_diff_PAN_PRD_l_turnout' 'l_diff_PAN_PRD_local' 'l_diff_PAN_PRD_ldist_city' ...
% 'lpop_l_turnout' 'lpop_local' 'lpop_ldist_city' ...
% 'l_turnout_local' 'l_turnout_ldist_city' ...
% 'local_ldist_city' 'ldist_pri' 'gov_PRI' 'ldist_pan' 'gov_PAN' 'ldist_prd' 'gov_PRD'};


%Remember: when x variables change, code for Z_PRI, Z_PAN, and X need to be
%adjusted.

    Y_PRI=3-Y(:,1);
    Y_PAN=3-Y(:,2);
    Y_PRD=3-Y(:,3);
    Z_PRD=X(:,end-1:end);
    Z_PAN=X(:,end-3:end-2);
    Z_PRI=X(:,end-5:end-4);

    X=X(:,1:end-6);

    betas=cell(1,3);
    phats=cell(1,3);
    
if nargin<3
        
    betas{1,1}=mnrfit([X Z_PRI],Y_PRI);
    phats{1,1}=mnrval(betas{1,1},[X Z_PRI]);

    betas{1,2}=mnrfit([X Z_PAN],Y_PAN);
    phats{1,2}=mnrval(betas{1,2},[X Z_PAN]);
    
    betas{1,3}=mnrfit([X Z_PRD],Y_PRD);
    phats{1,3}=mnrval(betas{1,3},[X Z_PRD]);
    
else
   
    %State by state PAN
    
    CLv_u=unique(CLv_PAN);
    N_cl_PAN=length(CLv_u);
    phats_PAN=zeros(length(Y_PAN),3);

    
    for i=1:N_cl_PAN
        
        beta_PAN=mnrfit([X(CLv_PAN==CLv_u(i),:) Z_PAN(CLv_PAN==CLv_u(i),:)],Y_PAN(CLv_PAN==CLv_u(i),:));
        phats_PAN(CLv_PAN==CLv_u(i),:)=mnrval(beta_PAN,[X(CLv_PAN==CLv_u(i),:) Z_PAN(CLv_PAN==CLv_u(i),:)]);            
        
    end
    
    phats{1,2}=phats_PAN;

    %State by State PRI
    
    CLv_u=unique(CLv_PRI);
    N_cl_PRI=length(CLv_u);
    phats_PRI=zeros(length(Y_PRI),3);
    
    for i=1:N_cl_PRI
        
        beta_PRI=mnrfit([X(CLv_PRI==CLv_u(i),:) Z_PRI(CLv_PRI==CLv_u(i),:)],Y_PRI(CLv_PRI==CLv_u(i),:));
        phats_PRI(CLv_PRI==CLv_u(i),:)=mnrval(beta_PRI,[X(CLv_PRI==CLv_u(i),:) Z_PRI(CLv_PRI==CLv_u(i),:)]);    
               
    end
    
    phats{1,1}=phats_PRI;

    
    %State by State PRD
    
    CLv_u=unique(CLv_PRD);
    N_cl_PRD=length(CLv_u);
    phats_PRD=zeros(length(Y_PRD),3);
    
    for i=1:N_cl_PRD
        
        beta_PRD=mnrfit([X(CLv_PRD==CLv_u(i),:) Z_PRD(CLv_PRD==CLv_u(i),:)],Y_PRD(CLv_PRD==CLv_u(i),:));
        phats_PRD(CLv_PRD==CLv_u(i),:)=mnrval(beta_PRD,[X(CLv_PRD==CLv_u(i),:) Z_PRD(CLv_PRD==CLv_u(i),:)]);    
               
    end
    
    phats{1,3}=phats_PRD;    
    
    
    %Remember: phats column order: 1)full rep. prob, 2)medium rep. prob, 3)low rep. prob.
    %PRI probabilities in first cell, PAN in second, PRD in third
    
end
    
end




